/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-04-27     RT-Thread    first version
 */

#include <rtthread.h>
#include "drv_common.h"

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>


/* --------------------------------------> GPIO Interrupt Test <-------------------------------------- */
#include <drivers/pin.h>

/* See drv_gpio.c file. */
/* PA1 */
#define EC200U_IRQ_PIN (1)
/* PB4 */
#define R_LIGHT_PWR_DEC_PIN (20)
/* PB5 */
#define MCU_BMS_LVD_PIN (21)
/* PC6 */
#define PHASE_INT_PIN (38)
/* PC7 */
#define ACC_DET_PIN (39)
/* PC9 */
#define BT_INT_PIN (41)
/* PB10 */
#define L_LIGHT_PWR_DEC_PIN (26)
/* PA11 */
#define DC72V_OUT_DET_PIN (11)
/* PA12 */
#define GS_INT_PIN (12)
/* PA15 */
#define CAN_INT (15)

void EC200U_IRQ_PIN_INT_CB(void *parameter)
{
    LOG_I("EC200U_IRQ_PIN interrupt! status: %d", rt_pin_read(EC200U_IRQ_PIN));
    // LOG_I("This ACC interrupt!");
}

void R_LIGHT_PWR_DEC_PIN_INT_CB(void *parameter)
{
    LOG_I("R_LIGHT_PWR_DEC_PIN interrupt! status: %d", rt_pin_read(R_LIGHT_PWR_DEC_PIN));
    // LOG_I("This ACC interrupt!");
}

void MCU_BMS_LVD_PIN_INT_CB(void *parameter)
{
    LOG_I("MCU_BMS_LVD_PIN interrupt! status: %d", rt_pin_read(MCU_BMS_LVD_PIN));
    // LOG_I("This ACC interrupt!");
}

void PHASE_INT_PIN_INT_CB(void *parameter)
{
    LOG_I("PHASE_INT_PIN interrupt! status: %d", rt_pin_read(PHASE_INT_PIN));
    // LOG_I("This ACC interrupt!");
}

void ACC_DET_PIN_INT_CB(void *parameter)
{
    LOG_I("ACC_DET_PIN interrupt! status: %d", rt_pin_read(ACC_DET_PIN));
    // LOG_I("This ACC interrupt!");
}

void BT_INT_PIN_INT_CB(void *parameter)
{
    LOG_I("BT_INT_PIN interrupt! status: %d", rt_pin_read(BT_INT_PIN));
    // LOG_I("This ACC interrupt!");
}

void L_LIGHT_PWR_DEC_PIN_INT_CB(void *parameter)
{
    LOG_I("L_LIGHT_PWR_DEC_PIN interrupt! status: %d", rt_pin_read(L_LIGHT_PWR_DEC_PIN));
    // LOG_I("This ACC interrupt!");
}

void DC72V_OUT_DET_PIN_INT_CB(void *parameter)
{
    LOG_I("DC72V_OUT_DET_PIN interrupt! status: %d", rt_pin_read(DC72V_OUT_DET_PIN));
    // LOG_I("This ACC interrupt!");
}

void GS_INT_PIN_INT_CB(void *parameter)
{
    LOG_I("GS_INT_PIN interrupt! status: %d", rt_pin_read(GS_INT_PIN));
    // LOG_I("This ACC interrupt!");
}

void CAN_INT_INT_CB(void *parameter)
{
    LOG_I("CAN_INT interrupt! status: %d", rt_pin_read(CAN_INT));
    // LOG_I("This ACC interrupt!");
}

void Test_Use_Pin_Interrupt(void)
{
    rt_pin_mode(EC200U_IRQ_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(R_LIGHT_PWR_DEC_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(MCU_BMS_LVD_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(PHASE_INT_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(ACC_DET_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(BT_INT_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(L_LIGHT_PWR_DEC_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(DC72V_OUT_DET_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(GS_INT_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(CAN_INT, PIN_MODE_INPUT_PULLUP);

    if ((rt_pin_attach_irq(EC200U_IRQ_PIN, PIN_IRQ_MODE_RISING_FALLING, EC200U_IRQ_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(EC200U_IRQ_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open EC200U_IRQ_PIN interrupt failed!");
    }

    if ((rt_pin_attach_irq(R_LIGHT_PWR_DEC_PIN, PIN_IRQ_MODE_RISING_FALLING, R_LIGHT_PWR_DEC_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(R_LIGHT_PWR_DEC_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open R_LIGHT_PWR_DEC_PIN interrupt failed!");
    }

    if ((rt_pin_attach_irq(MCU_BMS_LVD_PIN, PIN_IRQ_MODE_RISING_FALLING, MCU_BMS_LVD_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(MCU_BMS_LVD_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open MCU_BMS_LVD_PIN interrupt failed!");
    }

    if ((rt_pin_attach_irq(PHASE_INT_PIN, PIN_IRQ_MODE_RISING_FALLING, PHASE_INT_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(PHASE_INT_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open PHASE_INT_PIN interrupt failed!");
    }

    if ((rt_pin_attach_irq(ACC_DET_PIN, PIN_IRQ_MODE_RISING_FALLING, ACC_DET_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(ACC_DET_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open ACC interrupt failed!");
    }

    if ((rt_pin_attach_irq(BT_INT_PIN, PIN_IRQ_MODE_RISING_FALLING, BT_INT_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(BT_INT_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open BT_INT_PIN interrupt failed!");
    }

    if ((rt_pin_attach_irq(L_LIGHT_PWR_DEC_PIN, PIN_IRQ_MODE_RISING_FALLING, L_LIGHT_PWR_DEC_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(L_LIGHT_PWR_DEC_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open L_LIGHT_PWR_DEC_PIN interrupt failed!");
    }

    if ((rt_pin_attach_irq(DC72V_OUT_DET_PIN, PIN_IRQ_MODE_RISING_FALLING, DC72V_OUT_DET_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(DC72V_OUT_DET_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open DC72V_OUT_DET_PIN interrupt failed!");
    }

    if ((rt_pin_attach_irq(GS_INT_PIN, PIN_IRQ_MODE_RISING_FALLING, GS_INT_PIN_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(GS_INT_PIN, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open GS_INT_PIN interrupt failed!");
    }

    if ((rt_pin_attach_irq(CAN_INT, PIN_IRQ_MODE_RISING_FALLING, CAN_INT_INT_CB, RT_NULL) != RT_EOK) ||
        (rt_pin_irq_enable(CAN_INT, PIN_IRQ_ENABLE) != RT_EOK))
    {
        LOG_E("Open CAN_INT interrupt failed!");
    }
}
/* --------------------------------------> GPIO Interrupt Test End <-------------------------------------- */



/* --------------------------------------> PWM Test <-------------------------------------- */

#include "drivers/rt_drv_pwm.h"

rt_thread_t Pwm_thread1;

#define PWM1_DEV "pwm1"
#define PWM1_DEV_CHANNEL 1 /* PWM通 道 */
#define PWM1_DEV_CHANNEL_N -1 /* PW互补M通道 */

struct rt_device_pwm *pwm_dev; /* PWM设 备 句 柄 */

void PWM_Thread(void *parameter)
{
    rt_uint32_t period, pulse;

    period = 1000 * 1000;           /* Unit: ns.  */
    pulse = 500 * 1000;             /* Unit: ns. */

    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM1_DEV);
    if (pwm_dev == RT_NULL)
    {
        LOG_E("pwm sample run failed! can't find %s device!\n", PWM1_DEV);
        goto EXIT_TASK;
    }
    /* 配置PWM */
    if (rt_pwm_set(pwm_dev, PWM1_DEV_CHANNEL, period, pulse) != RT_EOK)
    {
        LOG_E("rt_pwm_set ERROR");
        goto EXIT_TASK;
    }
    /*  使能PWM */
    if (rt_pwm_enable(pwm_dev, PWM1_DEV_CHANNEL) != RT_EOK)
    {
        LOG_E("rt_pwm_enable Error");
        goto EXIT_TASK;
    }
    /*  使能PWM互补 */
    if (rt_pwm_enable(pwm_dev, PWM1_DEV_CHANNEL_N) != RT_EOK)
    {
        LOG_E("rt_pwm_enable complementary Error");
        goto EXIT_TASK;
    }

    LOG_D("PWM device ready! find %s device!\n", PWM1_DEV);

    for (;;)
    {
        rt_thread_mdelay(50);
    }
EXIT_TASK:
    rt_thread_mdelay(50);
}

/* --------------------------------------> PWM Test End <-------------------------------------- */

int main(void)
{
    int count = 1;

    // Test_Use_Pin_Interrupt();

    // /* 创建 PWM_Thread 线程 */
    // Pwm_thread1 = rt_thread_create("PWM_Thread", PWM_Thread, RT_NULL, 2048, 10, 10);
    // /* 创建成功则启动线程 */
    // if (Pwm_thread1 != RT_NULL)
    // {
    //     LOG_D("Create PWM_Thread success.");
    //     rt_thread_startup(Pwm_thread1);
    // }

    while (count++)
    {
        // LOG_D("Hello RT-Thread!");
        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}
